Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Javascript - looking for most efficient way to convert a JSON object into a null (<0x00>)-terminated byte array

Per https://www.jsonrpc.org/specification I need a JSON object returned as a byte array, contatenated with <0x00>.

Consider this object:

{"jsonrpc":"2.0","method":"StatusGet","id":"1","params":0}

Returned as <0x00>-terminated byte array:

[123,34,106,115,111,110,114,112,99,34,58,34,50,46,48,34,44,34,109,101,116,104,111,100,34,58,34,83,116,97,116,117,115,71,101,116,34,44,34,105,100,34,58,34,49,34,44,34,112,97,114,97,109,115,34,58,48,125,0]

From examples found elsewhere, here's what I currently have that works (in a Nodered function node, which is fed into a TCP request node):

function fnStringMessage(str){
    
    let bytes = Buffer.from(str)
    bytes = []
    for (let i=0; i<str.length; i++){
        bytes.push(str.charCodeAt(i))
    }
    bytes.push(0)

    return (bytes)
}

let ss = JSON.stringify(msg.payload)

msg.payload = Buffer.from(fnStringMessage(ss))

return msg;

However I'm a noob and using a for loop looks a bit turtle to me. Looking for the hare, and any other suggestions to simplify this further. Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can append a zero byte to the string, then convert that into a Buffer.

function fnStringMessage(str){
  return Buffer.from(str + "\0");
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda